﻿<div>
    <Steps Current="current">
        @if (Model.Items != null && Model.Items.Count > 0)
        {
            foreach (var item in Model.Items)
            {
                <Step Title="@item.Title" Subtitle="@item.SubTitle" Description="@item.Description" />
            }
        }
    </Steps>
    @if (Model.IsContent)
    {
        <div class="steps-content">
            @Model.Items[current].Content
        </div>
        <div class="form-action">
            @if (current > 0)
            {
                <Button Type="primary" OnClick="OnPrevClick">上一步</Button>
            }
            @if (current < Model.Items.Count - 1)
            {
                <Button Type="primary" OnClick="OnNextClick">下一步</Button>
            }
            @if (current == Model.Items.Count - 1 && !Model.IsView)
            {
                <Button Type="primary" OnClick="OnComplete">完成</Button>
            }
        </div>
    }
</div>

@code {
    private int current = 0;

    [Parameter] public StepModel Model { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
        current = Model.GetCurrentIndex();
    }

    private async void OnPrevClick()
    {
        if (!await SaveAsync())
            return;

        current--;
    }

    private async void OnNextClick()
    {
        if (!await SaveAsync())
            return;

        current++;
    }

    private async void OnComplete()
    {
        await SaveAsync(true);
    }

    private async Task<bool> SaveAsync(bool isClose = false)
    {
        if (Model.IsView)
            return true;

        return await Model.OnSave?.Invoke(isClose);
    }
}